Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to fix this script that sometimes doesn't detect it was a player that touched it?

Asked by 4 years ago

I sometimes get this error: FireClient: player argument must be a player object and it's just sometimes that this happens, and it doesn't detect it was a player even tho it was. This fires the event: (server script)

local brick = script.Parent
local brickTouchedEvent = game.ReplicatedStorage.Events.BrickTouched
local debounce = false
brick.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        brickTouchedEvent:FireClient(player)
        print(player.Name.."opened the gui!")
        wait(4)
        debounce = false
    end
end)

And this sets what the event does: (local script)

brickTouchedEvent.OnClientEvent:Connect(function()
    gui.Enabled = true
    gui.Frame.bmw.GoLeftShowHOON.Visible = true
    gui.Frame.bmw.GoRightShowPOR.Visible = true
    gui.Frame.Close.Visible = true
    camera.CameraType = Enum.CameraType.Scriptable
    camera.CFrame = workspace.Cameras.BMWPart.CFrame
    char.Humanoid.WalkSpeed = 0
    script.Disabled = true
    end)

3 answers

Log in to vote
1
Answered by 4 years ago

You need to have a check that makes sure the part that it has touched is actually the player.

Do something like this

local Event = game.ReplicatedStorage.Event
local Part = workspace.Part

Part.Touched:Connect(function(hit)
    if game.Players:FindFirstChild(hit.Parent.Name) then
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

        Event:FireClient(Player)
    end
end)
0
I agree his script was to clouded from the start with an easy fix shawn36589 0 — 4y
Ad
Log in to vote
0
Answered by
XDvvvDX 186
4 years ago
Edited 4 years ago

You said that something touched the brick. You can check if it's ClassName is Humanoid then run a function else just do nothing. Here's how you do it :

function onTouch(hit)
  if hit.Parent:IsA("Humanoid") then
    print("A player touched the brick.")
  else
    print("It was not a player who touched the brick.")
end

script.Parent.Touched:Connect(onTouch)

If you still have questions ask me!

0
Can you do the script for me? Because I absolutely suck with functions. User#32819 0 — 4y
0
Yes. I resent a message with the script. XDvvvDX 186 — 4y
Log in to vote
0
Answered by 4 years ago

When you are doing FireClient, you need to pass along a "player object" to specify what player should be targeted. I found this very complicated myself, so I basically sent the information to every client (so I wouldn't have to put in a player object), along with the targeted player name variable, playerName. In a client script, I said that if the local player's name was equal to that of the variable playerName, then it would run a function to kill the player.

-------SERVER SCRIPT-------

targetPlayer.FireAllClients(playerTargeted)--targetPlayer is the name of the `remoteEvent`, and you would define `playerTargeted` as the player you wanted to be given the info.

-------CLIENT SCRIPT-------

function killPlayer()
--[[

Insert the code to kill the player.

--]]
end

targetPlayer.OnClientEvent:Connect(killPlayer)

I hope this helped, and happy coding!

Answer this question