Creating a System that gives someone an item

So I put the local script within a remote event or within the players starter scripts?

Putting a LocalScript in a RemoteEvent would never work. I guess I need to explain you what are LocalScripts first. I promise I will make the explanation short and sweet! :grin:

LocalScripts, as the name suggests, work only on the player, which means that the server is not involved in this script whatsoever. So, for a LocalScript to work, it needs to be inside the player itself (doesn’t matter where but the player or the player’s character).

I hope I made it clear that now the LocalScript shouldn’t be in the RemoteEvent, it is supposed to be anywhere inside the player. You can place it anywhere: the StarterGui, the StarterPack, the PlayerScripts, the PlayerCharacter scripts and literally anywhere which is cloning inside the player

Yours,
pranvexploder :smiley:

I see, I put it into player scripts but it doesn’t seem to give the item to the player and also it has a red underline underneath remoteEvent image

Well, that’s because you need to create a RemoteEvent in the ReplicatedStorage and local it in both the scripts.

Add this line to the top of both the scripts:

Line
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")

It should work :smiley:

Through local scripts you can’t access ServerStorage, make sure it’s a ServerScript.

Also, don’t forget ipairs is the fastest generator currently, I recommend using index value pairs instead of key , value pair iteration, it will improve performance and speed at which the code is executed.

1 Like

i made already models to do that:
(Buyable tool)
link: https://www.roblox.com/library/4882718716/Buyable-Tools
Give Tool When click part (No Buyable)
link: https://www.roblox.com/library/4882665217/Give-Tool-On-Click-Part
if you want to add tools then just go to the tools folder and add your own tool :wink:
more info about that with the model.
@RomanEmpireThe1st

Actually, you can just move the tool to the ReplicatedStorage. Won’t it work?

you can move the tools also to serverstorage and is still going to work, same as i did with my models that i sent here: Creating a System that gives someone an item - #14 by REALINONOOBYT
@pranvexploder

Oh okay. I got confused reading @XxELECTROFUSIONxX 's post.

Ah ok that is really useful thank you, but I was wondering if there is a way to make it so that they could only click it once?

1 Like

you can do that so is check if the player already have the tool, and if the player already own the tool so is will print that the player already have the tool. :slight_smile:
hope is help you @RomanEmpireThe1st.

Yes that helped I made a check for that but it still removes the item from the player when they reset or refresh.

local folder = game.ServerStorage.Tools
local tool = folder.ParkourTrophy
local Parent = script.Parent.Parent
amount = 10
currencyname = "Points"

script.Parent.ClickDetector.MouseClick:Connect(function(player)
 	local Tool = player.Backpack:FindFirstChild("ParkourTrophy") or player.Character:FindFirstChild("ParkourTrophy")
	if Tool then
		return
	else local plr = game.Players:GetPlayerFromCharacter(player)
	local clone = tool:Clone()
	clone.Parent = player.Backpack
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") and v then
			v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
		end
		end
		end
end)

Well, I guess you could switch back to my older reply here.

Yeah I did but it doesn’t seem to give the item to the player when you click the block.

Script in block:

amount = 10
currencyname = "Points"

local tool = game.ServerStorage["ParkourTrophy"]
local klone = tool
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
script.Parent.ClickDetector.MouseClick:connect (function(plr)
if klone.Parent ~= plr.StarterPack then
remoteEvent:FireClient(plr, tool)
for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") and v then
			v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
		end
end
else
end
end)

Local Script in starter player scripts:

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
remoteEvent.OnClientEvent:Connect(function(tool)
local klone = tool:Clone()
klone.Parent = game:GetService("StarterPack")
end)

Remote event in Replicated Strogae and the Reward called “parkour trophy” in server storage.

Try changing these:

klone = tool to klone = tool:Clone()
remoteEvent:FireClient(plr, tool) to remoteEvent:FireClient(plr, klone)

And in the LocalScript, change these:

Remove the line: local klone = tool.Clone()
remoteEvent.OnClientEvent(tool) to remoteEvent.OnClientEvent(klone)
klone.Parent = game.StarterPack to klone.Parent = game.Players.LocalPlayer.Backpack

If it still doesn’t work, message me.

Edit: I clearly told you that plr.StarterPack won’t work, why are you still using the if statement to check if the tool is parented to the plr.StarterPack? That’s the issue.

@pranvexploder NO it won’t work, I said that if you use a local script you can’t access ServerStorage, as it can only be accessed Server - side.

Replicated Storage should be used when both the Client and Server have to access its contents, otherwise ServerStorage is not only a more secure method for storing stuff, but improves loading time too when used over Replicated Storage as it reduces Network traffic and isn’t replicated .

Well it does half of what it is supposed to do now that I replaced StarterPack with Backpack it now gives the points but not the tool trophy, would the reason for that be because I should put something other than Backpack?

Could you show me the current script?

Script:

amount = 10
currencyname = "Points"

local tool = game.ServerStorage["ParkourTrophy"]
local klone = tool:Clone()
local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
script.Parent.ClickDetector.MouseClick:connect (function(plr)
if klone.Parent ~= plr.Backpack then
remoteEvent:FireClient(plr, klone)
for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") and v then
			v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
		end
end
else
end
end)

Local Script:

local remoteEvent = game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent")
remoteEvent.OnClientEvent:Connect(function(klone)

klone.Parent = game:GetService("StarterPack")
end)
1 Like

So any idea what the issue is?