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

Tried to make an item giver that gives on touch and didn't work. Any help?

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)

1 answer

✓ Accepted answer

Hi Derpy,

I don't think .Touched event works in LocalScripts quite as well as ServerScripts. 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, LocalScripts 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.

####Analysis:

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)

####Here, I'll show you the changes you will have made afterwards:

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)

##Well, I hope I helped and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

2 comments

0

oh I see, thanks for helping man :D

0

Np

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