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 5 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

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

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

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)
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 — 5y
0
put a tool in replicatedstorage and name it "Tool" also put a remotevent in replicatedstorage and name that "toolevent" Imperialy 149 — 5y
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 — 5y
Ad

Answer this question