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

How do I make a script that gives player a tool?

I making a round system that gives the player a tool when the round starts heres my code:

this is the round script: it is a script in ServerSciptService


local giveTool = game.ReplicatedStorage.givetool.Value

while true do
	
	map = game.ReplicatedStorage.Maps.map1:Clone()
	map.Parent = game.Workspace

	giveTool = 1 -- Changes the value	

	wait(120) -- round time is 2 mins
	map:Destroy()

	map = game.ReplicatedStorage.Maps.map2:Clone
	map.Parent = game.Workspace

	giveTool = 0

	wait(120)
	map:Destroy()

end

ToolGiver script: it is a script in workspace:


game.ReplicatedStorage.givetool.Changed:Connect(function()

	local tool = game.Lighting.tools.tool1:Clone()
	
	tool.Parent = game.Players.LocalPlayer.Backpack

end)

What am i doing wrong? it wont give me the tool!

2 answers

Hi there, Server scripts cannot use LocalPlayer. Also, if you tried to add the tool locally it'd only show for the local player. You also don't need to use multiple scripts. Here's an example of what you can do.

function givePlayersTool()
local players = game.Players:GetChildren()
for i, v in pairs(players) do
local clone =  game.Lighting.tools.tool1:Clone()
clone.Parent = v.Backpack
end
end
while true do
    givePlayersTool()
    map = game.ReplicatedStorage.Maps.map1:Clone()
    map.Parent = game.Workspace
    wait(120)
    map:Destroy()
    map = game.ReplicatedStorage.Maps.map2:Clone
    map.Parent = game.Workspace
    givePlayersTool()
    wait(120)
    map:Destroy()
end


2 comments

0

If i wanted to delete the tool would i just put v.Backpack.tool:Destroy?

0

Yes.

HI! So the problem i noticed first was that you are calling the localplayer from a server script and not a local script. I recommend using a remote event and firing to all clients where the local script would be in the starterplayerscripts...

ServerScript:

local giveTool = game.ReplicatedStorage.givetool.Value

while true do
    map = game.ReplicatedStorage.Maps.map1:Clone()
    map.Parent = game.Workspace
    giveTool = 1
game.ReplicatedStorage.GiveToolEvent:FireAllClients(giveTool)
    wait(120) 
    map:Destroy()
    map = game.ReplicatedStorage.Maps.map2:Clone()
    map.Parent = game.Workspace
    giveTool = 0
    wait(120)
    map:Destroy()
end

LocalScript:

game.ReplicatedStorage.ToolGivingEvent.OnClientEvent:Connect(function(giveTool)
	local player = game:GetService("Players").LocalPlayer
	local Tool = game.Lighting.tools:FindFirstChild("tool"..giveTool):Clone()
	Tool.Parent = player.Backpack
end)

Let me know if that works!

2 comments

0

Where would i put the local script? Thanks for answering btw!

0

Put it in starter playerscripts. In the explorer, go to the very botton then open the drop down for StarterPlayer, then add a localscript to starterplayer scripts

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