如何在触碰部件时触发远程事件(Roblox Studio)

huangapple go评论97阅读模式
英文:

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) 

huangapple
  • 本文由 发表于 2023年6月8日 05:13:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427147.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定