Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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

01local tool = ("tool")
02function clicked(player)
03        local stats = player:FindFirstChild("Towers")
04        local sp = stats:FindFirstChild("Towers")
05        if sp == nil then return false end
06        if (sp.Value >= 1) then
07        tool:clone().parent = player.backpack
08        end
09end
10 
11game.Players.PlayerAdded:connect(function(player)
12  player.CharacterAdded:connect(function()
13    clicked(player)
14  end)
15end)

1 answer

Log in to vote
1
Answered by
Imperialy 149
6 years ago

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

01local replicatedstorage = game:GetService("ReplicatedStorage")
02local event = replicatedstorage:WaitForChild("toolevent") -- name this to the toolevent
03local tool = replicatedstorage:WaitForChild("Tool") -- toolname
04local button = script.Parent
05local player = game.Players.LocalPlayer
06local mouse = player:GetMouse()
07local stats = player:WaitForChild("Towers")
08local sp = stats:WaitForChild("Towers") -- considering this is your intvalue
09button.MouseButton1Click:Connect(function()
10    if sp.Value >= 1 then
11        event:FireServer(mouse.Hit)
12    end
13end)

Then insert a serverscript inside of serverscriptservice with the following text

01local replicatedstorage = game:GetService("ReplicatedStorage")
02local event = replicatedstorage:WaitForChild("toolevent") -- name this to the toolevent
03local tool = replicatedstorage:WaitForChild("Tool") -- toolname
04event.OnServerEvent:Connect(function(player,mouse)
05    local stats = player:WaitForChild("Towers")
06    local sp = stats:WaitForChild("Towers") -- considering this is your intvalue
07    if sp.Value >= 1 then
08        tool:Clone().Parent = player:WaitForChild("Backpack")
09        sp.Value = sp.Value - 1
10    end
11end)
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 voidofdeathfire 148 — 6y
0
put a tool in replicatedstorage and name it "Tool" also put a remotevent in replicatedstorage and name that "toolevent" Imperialy 149 — 6y
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 TheOnlySmarts 233 — 6y
Ad

Answer this question