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

Weapons not working when bought from shop gui, what to do?

Okay so I've made this gui (Using a complicated video tutorial that didn't totally work) But the buying stuff works, but when you buy the weapon (As an example a sword) it won't attack, what to do? Here is the script for the weapon and other stuff:

The sword item buying thingy script:

script.Parent.MouseButton1Click:connect(function()
 local RS = game:GetService("ReplicatedStorage")
 local item =  RS:WaitForChild("Sword")
 local price = 99 -- Change Your Price Here
 local player = game.Players.LocalPlayer
 local stats = player:WaitForChild("leaderstats")
 if stats.Money.Value >= price then -- Change the Money to your Currency Name
  stats.Money.Value = stats.Money.Value - price
  local cloned = item:Clone()
  local cloned2 = item:Clone()
  cloned.Parent = player.Backpack
  cloned2.Parent = player.StarterGear
 end
end)

Leaderboard:

game.Players.ChildAdded:connect(function(player)
 local stats = Instance.new("Model",player)
 stats.Name="leaderstats"
 local money = Instance.new("IntValue",stats)
 money.Name="Money"
 money.Value=300 -- Change Your Starting Money
 player.CharacterAdded:connect(function(character)
  character:WaitForChild("Humanoid").Died:connect(function()
  local tag = character.Humanoid:FindFirstChild("creator")
   if tag ~= nil then
    if tag.Value ~= nil then
     local stats = tag.Value:WaitForChild("leaderstats")
stats["Money"].Value = stats ["Money"].Value + 5
    end
   end
  end)
 end)
end)

I also put the weapon item in ReplicatedStorage. Thanks - keketin

3 comments

0

Wow!

0

Please post the script for the sword, because if it isn't attacking, then the problem is probably in the sword, not in theses lines of code

0

I've told you the problem over Discord (Just so people are not thinking "Why isn't he answering". xd)

1 answer

✓ Accepted answer

first go to your replicated storage, make a new RemoteEvent named BuyEvent

and replace your gui script with


script.Parent.MouseButton1Click:connect(function()
 	local RS = game:GetService("ReplicatedStorage")
	local BuyEvent = RS:WaitForChild("BuyEvent")
	local item =  RS:WaitForChild("Sword")
	local price = 99 -- Change Your Price Here
	local player = game.Players.LocalPlayer
	local stats = player:WaitForChild("leaderstats")
	if stats.Money.Value >= price then -- Change the Money to your Currency Name
		BuyEvent:FireServer(item, price) -- Calling a remote event passes the client who fired it automatically
	end
end)

then make a new script in ServerScriptService

local BuyEvent = game.ReplicatedStorage.BuyEvent

local function buyEventFunction(player, item, price)
	local playerStats = game.Players[player.Name].leaderstats
	playerStats.Money.Value = playerStats.Money.Value - price
	local cloned = item:Clone()
	local cloned2 = item:Clone()
	cloned.Parent = player.Backpack
	cloned2.Parent = player.StarterGear

end

BuyEvent.OnServerEvent:Connect(buyEventFunction)

this should work, but some of the names might be misspelled

to use this anywhere else, just use this template and replace the names

theBuyEventVariable:FireServer(item_that_is_being_bought, the_price)

I have made a change in both scripts so you will want to updated both to the new ones

1 comment

0

Thank you so much!!!!!!!!!! Finally works, can I do this with other weapons / items also? Or do I need to change something then?

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