So i tried making a giver that gives tool to a player when touched. I used a Local Script cause I need the LocalPlayer.
local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer") local giver = script.Parent gave = false giver.Touched:connect(function(hit) if hit:FindFirstChild("Humanoid") then if not gave then local playerHammer = hammer:Clone() playerHammer.Parent = game.Players.LocalPlayer.Backpack gave = true end end end)
Hi Derpy,
.Touched
event works in LocalScript
s quite as well as ServerScript
s. You don't need local player. You can just put the script in a ServerScript but, you need to make it in the Server. Also, LocalScript
s don't work in the server, so I have no idea why you're using a LocalScript
in the server. I mean, you must be using a LocalScript
in the server because the giver is the parent of the LocalScript
, and the giver must be in the Server, in this case the workspace, to be seen. So, just replace the LocalScript
with a ServerScript
. Also, you need to fix up your code a bit because it's wrong. Here, I'll give you an analysis of it down below.local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer") -- Ok. local giver = script.Parent gave = false -- You do not need a boolean. giver.Touched:connect(function(hit) -- Use ':Connect()' because, ':connect' is deprecated. if hit:FindFirstChild("Humanoid") then -- Change that to 'if hit.Parent:FindFirstChild("Humanoid") because the hit will be the part under the character that's hit, not the character itself. if not gave then -- Again, you don't need a boolean. Instead, just check if the tool exists in the backpack. local playerHammer = hammer:Clone() playerHammer.Parent = game.Players.LocalPlayer.Backpack -- Again, you don't need local scripts. gave = true -- Don't need a boolean. end -- if statement for this end is not necessary, so you will need to delete this too with the if statement. end end)
local hammer = game:GetService("ServerStorage"):WaitForChild("Hammer"); local players = game:GetService("Players"); local giver = script.Parent; giver.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then local player = players:GetPlayerFromCharacter(hit.Parent); local backpack = player:WaitForChild("Backpack"); if not backpack:FindFirstChild(hammer.Name) then local playerHammer = hammer:Clone() playerHammer.Parent = backpack; end end end)
Thanks,
Best regards,
~~ KingLoneCat