Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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)

1 comment

0

I agree his script was to clouded from the start with an easy fix

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!

2 comments

0

Can you do the script for me? Because I absolutely suck with functions.

0

Yes. I resent a message with the script.

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!

This discussion is preserved as it was. New answers, comments, and votes are closed.