So i tried making a giver that gives tool to a player when touched. I used a Local Script cause I need the LocalPlayer.
01 | local hammer = game:GetService( "ServerStorage" ):WaitForChild( "Hammer" ) |
02 | local giver = script.Parent |
03 | gave = false |
04 |
05 | giver.Touched:connect( function (hit) |
06 | if hit:FindFirstChild( "Humanoid" ) then |
07 | if not gave then |
08 | local playerHammer = hammer:Clone() |
09 | playerHammer.Parent = game.Players.LocalPlayer.Backpack |
10 | gave = true |
11 | end |
12 | end |
13 | 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.01 | local hammer = game:GetService( "ServerStorage" ):WaitForChild( "Hammer" ) -- Ok. |
02 | local giver = script.Parent |
03 | gave = false -- You do not need a boolean. |
04 |
05 | giver.Touched:connect( function (hit) -- Use ':Connect()' because, ':connect' is deprecated. |
06 | 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. |
07 | if not gave then -- Again, you don't need a boolean. Instead, just check if the tool exists in the backpack. |
08 | local playerHammer = hammer:Clone() |
09 | playerHammer.Parent = game.Players.LocalPlayer.Backpack -- Again, you don't need local scripts. |
10 | gave = true -- Don't need a boolean. |
11 | end -- if statement for this end is not necessary, so you will need to delete this too with the if statement. |
12 | end |
13 | end ) |
01 | local hammer = game:GetService( "ServerStorage" ):WaitForChild( "Hammer" ); |
02 | local players = game:GetService( "Players" ); |
03 | local giver = script.Parent; |
04 |
05 | giver.Touched:Connect( function (hit) |
06 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
07 | local player = players:GetPlayerFromCharacter(hit.Parent); |
08 | local backpack = player:WaitForChild( "Backpack" ); |
09 |
10 | if not backpack:FindFirstChild(hammer.Name) then |
11 | local playerHammer = hammer:Clone() |
12 |
13 | playerHammer.Parent = backpack; |
14 | end |
15 |
16 | end |
17 | end ) |
Thanks,
Best regards,
~~ KingLoneCat