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 5 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)

01local brick = script.Parent
02local brickTouchedEvent = game.ReplicatedStorage.Events.BrickTouched
03local debounce = false
04brick.Touched:Connect(function(hit)
05    if not debounce then
06        debounce = true
07        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
08        brickTouchedEvent:FireClient(player)
09        print(player.Name.."opened the gui!")
10        wait(4)
11        debounce = false
12    end
13end)

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

01brickTouchedEvent.OnClientEvent:Connect(function()
02    gui.Enabled = true
03    gui.Frame.bmw.GoLeftShowHOON.Visible = true
04    gui.Frame.bmw.GoRightShowPOR.Visible = true
05    gui.Frame.Close.Visible = true
06    camera.CameraType = Enum.CameraType.Scriptable
07    camera.CFrame = workspace.Cameras.BMWPart.CFrame
08    char.Humanoid.WalkSpeed = 0
09    script.Disabled = true
10    end)

3 answers

Log in to vote
1
Answered by 5 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

01local Event = game.ReplicatedStorage.Event
02local Part = workspace.Part
03 
04Part.Touched:Connect(function(hit)
05    if game.Players:FindFirstChild(hit.Parent.Name) then
06        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
07 
08        Event:FireClient(Player)
09    end
10end)
0
I agree his script was to clouded from the start with an easy fix shawn36589 0 — 5y
Ad
Log in to vote
0
Answered by
XDvvvDX 186
5 years ago
Edited 5 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 :

1function onTouch(hit)
2  if hit.Parent:IsA("Humanoid") then
3    print("A player touched the brick.")
4  else
5    print("It was not a player who touched the brick.")
6end
7 
8script.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 — 5y
0
Yes. I resent a message with the script. XDvvvDX 186 — 5y
Log in to vote
0
Answered by 5 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-------

1targetPlayer.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-------

1function killPlayer()
2--[[
3 
4Insert the code to kill the player.
5 
6--]]
7end
8 
9targetPlayer.OnClientEvent:Connect(killPlayer)

I hope this helped, and happy coding!

Answer this question