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

Why don't I get a sword when I press the button?

I was wondering if this script would work for a shop GUI it will check if you have the required stats(In this case checking weather you bought or not since this is using datastore I used Intvalues.) So i was wondering what I need to change to give me the item

local tool = ("tool")
function clicked(player)
	    local stats = player:FindFirstChild("Towers") 
		local sp = stats:FindFirstChild("Towers") 
		if sp == nil then return false end 
		if (sp.Value >= 1) then 
		tool:clone().parent = player.backpack
		end
end

game.Players.PlayerAdded:connect(function(player)
  player.CharacterAdded:connect(function()
    clicked(player)
  end)
end)

1 answer

✓ Accepted answer

You have to use remote events; insert a localscript in the button with the following text:

local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage:WaitForChild("toolevent") -- name this to the toolevent
local tool = replicatedstorage:WaitForChild("Tool") -- toolname
local button = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local stats = player:WaitForChild("Towers")
local sp = stats:WaitForChild("Towers") -- considering this is your intvalue
button.MouseButton1Click:Connect(function()
	if sp.Value >= 1 then
		event:FireServer(mouse.Hit)
	end
end)

Then insert a serverscript inside of serverscriptservice with the following text

local replicatedstorage = game:GetService("ReplicatedStorage")
local event = replicatedstorage:WaitForChild("toolevent") -- name this to the toolevent
local tool = replicatedstorage:WaitForChild("Tool") -- toolname
event.OnServerEvent:Connect(function(player,mouse)
	local stats = player:WaitForChild("Towers")
	local sp = stats:WaitForChild("Towers") -- considering this is your intvalue
	if sp.Value >= 1 then
		tool:Clone().Parent = player:WaitForChild("Backpack")
		sp.Value = sp.Value - 1
	end
end)

3 comments

0

Wait what do I put in tools it wont let me change it I have more than 1 tool and this script is a little confusing to understand

0

put a tool in replicatedstorage and name it "Tool" also put a remotevent in replicatedstorage and name that "toolevent"

0

Good job but next time explain more about RemoteEvents, we can't just code their script for themselves, we have to provide a little bit more information on how RemoteEvents work, but you did really good. + 1

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