英文:
How to fire a remote event when a part is touched (Roblox Studio)
问题
I'm trying to make it so when you touch a block in roblox studio it fires a remote event that tells A Gui that it can show up and tell the player they are touching the block. Here is the server script.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function partTouched(obj)
if obj.Parent:findFirstChild("Humanoid") and obj.Parent.Name ~= "NextBot" then
print(obj.Parent) -- prints my username
local player = obj.Parent
ReplicatedStorage.Insafety:FireClient(player)
else
print("Not alive... :(")
end
end
script.Parent.Touched:connect(partTouched)
but every time it throws up the error FireClient: player argument must be a Player object - Server - Safezone:7
Thanks for any help!
英文:
I'm trying to make it so when you touch a block in roblox studio it fires a remote event that tells A Gui that it can show up and tell the player they are touching the block. Here is the server script.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function partTouched(obj)
if obj.Parent:findFirstChild("Humanoid") and obj.Parent.Name ~= "NextBot" then
print(obj.Parent) -- prints my username
local player = obj.Parent
ReplicatedStorage.Insafety:FireClient(player)
else
print("Not alive... :(")
end
end
script.Parent.Touched:connect(partTouched)
but every time it throws up the error FireClient: player argument must be a Player object - Server - Safezone:7
Thanks for any help!
答案1
得分: 0
错误提示您,在调用Insafety:FireClient(player)
时,player
对象不是一个Player。
您将玩家的Character模型(游戏世界中移动的对象)误认为是Player对象本身(代表加入游戏的Roblox玩家的对象)。
尝试使用Players:GetPlayerFromCharacter()函数,在Touched事件中轻松获取Player对象:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function partTouched(obj)
-- 如果NextBot触碰到这个部分,则返回
if obj.Parent.Name == "NextBot" then
return
end
-- 检查是否有玩家触碰到部分
local player = Players:GetPlayerFromCharacter(obj.Parent)
if player then
-- 向触碰到部分的玩家触发事件
ReplicatedStorage.Insafety:FireClient(player)
end
end
script.Parent.Touched:Connect(partTouched)
英文:
The error is telling you that when you call Insafety:FireClient(player)
, the player
object is not a Player.
You are mistaking the player's Character model (the thing that runs around the game world) for the Player object itself (the object that represents the Roblox player that joined the game).
Try using the Players:GetPlayerFromCharacter() function to easily get the Player from the character model in a Touched event :
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function partTouched(obj)
-- escape if NextBot touches this
if obj.Parent.Name == "NextBot" then
return
end
-- Check if a player has touched the part
local player = Players:GetPlayerFromCharacter(obj.Parent)
if player then
-- fire the event to the player that touched the part
ReplicatedStorage.Insafety:FireClient(player)
end
end
script.Parent.Touched:Connect(partTouched)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论