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

ClickDetector not working in servers?

I have this script (not a "Local Script") in a click detector and it works perfectly fine in local but when i go in a server it wont work, no errors in the output. Any help?

repeat wait(2)
player = game.Players.LocalPlayer
until player
function onClicked()
	local a = player.PlayerGui
	local gui = script.Parent.HatGui:Clone()
	gui.Parent = player.PlayerGui
	player.Character.Humanoid.WalkSpeed = 0
	gui.Frame.Col1.Disabled = false
	gui.Frame.Main.Disabled = false
	local cam = workspace.CurrentCamera
	cam.CameraSubject = player.Character.Head
	cam.CameraType = "Scriptable"
	local target = player.Character.Head
	cam.CoordinateFrame=CFrame.new(target.Position) * CFrame.Angles(0, math.rad(0), 0) * CFrame.new(4, 0, 4)
end
script.Parent.MouseClick:connect(onClicked)

1 comment

1

if it's not a local script then line 2 will not work

2 answers

✓ Accepted answer

##Problem

LocalPlayer does not work in Server Scripts. The LocalPlayer to the server is nil, since all players in the server can not necessarily be a "LocalPlayer". To a local script it may be as they work for the client or player.


##Solution

The ClickDetector will automatically get the player that clicked the part. It's one of the parameters the event returns. If you were to go to the wiki on the MouseClick Event you will see parameters within the parenthesis being the arguments the event will provide to the function.

All you simply have to do is utilize the argument provided through a variable for the function.


##Solution

function onClicked(player) --All you have to do is utilize the argument the event provides as a parameter of the function. The parameter name being player which you may use as a variable throughout the function.
    local a = player.PlayerGui
    local gui = script.Parent.HatGui:Clone()
    gui.Parent = player.PlayerGui
    player.Character.Humanoid.WalkSpeed = 0
    gui.Frame.Col1.Disabled = false
    gui.Frame.Main.Disabled = false
    local cam = workspace.CurrentCamera
    cam.CameraSubject = player.Character.Head
    cam.CameraType = "Scriptable"
    local target = player.Character.Head
    cam.CoordinateFrame=CFrame.new(target.Position) * CFrame.Angles(0, math.rad(0), 0) * CFrame.new(4, 0, 4)
end
script.Parent.MouseClick:connect(onClicked)

######Hopefully this answered your question, if it did do not forget to hit the accept answer button. If you have any other questions feel free to comment below and I will get back to you.

use

game.Players.PlayerAdded:connect(function(player)
	print("A player has entered: " .. player.Name)
end)

instead of

player = game.Players.LocalPlayer

on line 2

3 comments

0

oh wait, now it justs copy the gui to everyone in the server! D:

0

is it a gui or a brick in workspace thats being clicked?

0

its a brick but now i got it working anyway, Thanks!

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