I've tried this code, although it doesn't work.
script.Parent.Touched:Connect(function(hit) print("Hit") if hit.Parent.Humanoid then print("It was a player") if hit.Parent.leaderstats.Bucks.Value < 10 then print("You do not have enough bucks to purcahse a sword") elseif hit.Parent.leaderstats.Bucks.Value > 10 then print("You have enough bucks to purcahse a sword") local sword = game.ReplicatedStorage.ClassicSword:Clone() sword.Parent = hit.Parent.Backpack print("You have been given the sword") hit.Parent.leaderstats.Bucks.Value = hit.Parent.leaderstats.Bucks.Value -10 print("You have payed for the sword") end end end)
Does anybody know what's wrong with it?
It's simple. Here's a very short tutorial on how to do that.
1) We'll store a tool inside of ReplicatedStorage, I stored a hammer called "hammer ".
2) We'll now add the brick that gives the tool when touch.
3) We'll now add a remote event to ReplicatedStorage, and name it "RemoteGiveTool".
4) Now, we'll add a local script to the part, and type this :
function onTouch(hit) if hit.Parent.Humanoid ~= nil then game.ReplicatedStorage.RemoteGiveTool:FireServer() end script.Parent.Touched:Connect(onTouch)
5) Now, we'll add a Script in ServerScriptService which is what the remote does when it's fired, and type this :
game.ReplicatedStorage.RemoteGiveTool.OnServerEvent:Connect(function(player) local cloneOfTool = game.ReplicatedStorage.hammer:Clone() cloneOfTool.Parent = player.Backpack end
6) We're done! Now whenever you touch that brick it will clone the tool we placed in ReplicatedStorage and place it in the player's backpack. Hope it helped!
As far as your script goes, basically a lot of things went wrong. For example, I dont remember leaderstats existing in the character(hit.Parent), but rather the player of the character; So you should have searched for leadersats in the player of the character instead(you did put leaderstats in the player, right?). In addition, the way you checked for the money(player.leaderstats.Bucks.Value > 10) would be saying that you had to have over 10 dollars(e.g $10.1) to purchase the sword worth $10. The appropriate way to check for if the player has equal to or over $10 would be to use ">=" instead of ">". I made a rough edit of your code, which should work; though i cant assure you that:
local function getPlayerFromCharacter(character) for _, player in pairs(game:GetService("Players"):GetPlayers()) do if player.Character == character then return player end end end script.Parent.Touched:Connect(function(hit) if hit.Parent.Humanoid then print("it was a player") local player = getPlayerFromCharacter(hit.Parent) if player.leaderstats.Bucks.Value < 10 then print("u broke boi") elseif player.leaderstats.Bucks.Value >= 10 then print("purchasing sword...") local sword = game.ReplicatedStorage.ClassicSword:Clone() sword.Parent = player.BackPack print("sword acquired") player.leaderstats.Bucks.Value = hit.Parent.leaderstats.Bucks.Value - 10 print("sword has been paid for") end end end)
im sorry im rusty at this