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

How do i use a localscript within a serverscript?

Asked by
funzrey 58
9 years ago

Okay guys, I'm making a game and i have a clickable unioned pickaxe, but it requires a server-script to run, but a local-script to add the tool to the player's backpack, The tool is in game.Lighting so it can clone it and add it to the user backpack, please help! --My Code-- ServerScript (game.Workspace.Union.Script)

script.Parent.ClickDetector.MouseHoverEnter:connect(function()
    script.Parent.SelectionBox.Transparency = 0.5
end)
script.Parent.ClickDetector.MouseHoverLeave:connect(function()
    script.Parent.SelectionBox.Transparency = 1
end)
script.Parent.ClickDetector.MouseClick:connect(function()
--I cannot get the LocalPlayer or anything like that within a server script, which is where i am stuck

end)

LocalScript (game.StarterPack)

pickaxeClicked = game.Workspace.GlobalVars.picaxeclicked
while pickaxeClicked == false do --to keep the code running until pickaxe gets clicked (maybe --thats where i failed? idk)
if pickaxeClicked == true then
pickaxeTool = game.Lighting.Pickaxe:Clone()
pickaxeTool.Parent = game.Players.LocalPlayer.Backpack
end
wait(0.11)
end

So its pretty much just simple code.. but i cant figure out how to get the whole thing to work. Please help.

0
What are you trying to do? WHY isnt that a server script? you can clone it to the player from the serverscript. iaz3 190 — 9y

1 answer

Log in to vote
1
Answered by
iaz3 190
9 years ago

Your code is unnecessarily complicated, you can just use the serverscript. You don't need the localscript.

MouseClick GIVES you the player who clicked, so you can use that to clone.

The below code should work just fine.

script.Parent.ClickDetector.MouseHoverEnter:connect(function()
    script.Parent.SelectionBox.Transparency = 0.5
end)
script.Parent.ClickDetector.MouseHoverLeave:connect(function()
    script.Parent.SelectionBox.Transparency = 1
end)
script.Parent.ClickDetector.MouseClick:connect(function(playerWhoClicked)
    pickaxeTool = game.Lighting.Pickaxe:Clone()
    pickaxeTool.Parent = playerWhoClicked.Backpack
end)

Alternatively, if you REALLY need to have a localscript run, you can clone it into the players character/backpack/playerGUI like this

script.Parent.ClickDetector.MouseClick:connect(function(player)
    someLocalScript = game.Workspace.LocalScript  -- This is some sort of useful script, stored wherever, disabled. Local.
    clonedLocalScript = someLocalScript:clone()
    clonedLocalScript.Parent = player.BackPack
    clonedLocalScript.Disabled = false -- So it runs.
end)
1
Thanks for the help (sorry i was inactive) funzrey 58 — 8y
Ad

Answer this question