Search code examples
luascriptinggame-developmentroblox

How do I make a sell script? (Roblox)


I have been making a Roblox simulator as a side project to learn how to make games, Now I have gotten some help as you can see below but it is still not working. I have got some pictures and videos in these links: https://flickr.com/photos/195497771@N05 https://vimeo.com/user173767075

Currently my issue is still the title of my question and my tool not working. I changed it to a remote event and now it stopped changing my leaderstats. I have tried a bunch of different solutions but none work. My first script is my tool script. Here it is:

local player = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
    if player.Debounce.Value == false then
        game.ReplicatedStorage.Power:FireServer(script.Parent.Values)
    
local action = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.Animation)
        
        action:Play()
    end
end)

Now, that is in a LocalScript and that works. It prints that it activated and the animation plays. Now my problem is with a script in ServerScriptService not receiving the remote event. I have a print in it but nothing happens. Here is that script:

game.ReplicatedStorage.Power.OnServerEvent:Connct(function(player, valueFolder)
    if player.Debounce.Value == false then
        player.leaderstats.Sticks.Value += valueFolder.Power.Value
        player.Debounce.Value = true
        wait(valueFolder.Cooldown.Value)
        player.Debounce.Value = false
    end
end)

So, for my original question, the sell knows I touch it and it activates the event but nothing changes in the leaderstats. Here is my script to detect when the player touches the sell part.

local sellevent = game.ReplicatedStorage.Sell

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        print("sell fired")
        sellevent:Fire()
    end
end)

That script works. But that is a server script. Let me know if I should change it to a LocalScript. Now, here is my script that changes the leaderstats, it detects the the event fired but nothing changes in leaderstats.

game.ReplicatedStorage.Sell.Event:Connect(function(player)
    print("bindable event fired")
    if player ~= nil then
        if player.leaderstats.Sticks.Value > 0 then
            player.leaderstats.Cash.Value += player.leaderstats.Sticks.Value
            player.leaderstats.Sticks.Value = 0
        end
    end
end)

I gave as much information as I could. If you need anything else I can get more pictures or videos but that is the best I could give. Any help appreciated!


Solution

  • Okay, after watching the video, I might have a hunch as to what's going wrong. The code in your original question works just fine. If you were to print out the Sticks.Value before adding it to Cash.Value, you'd see that Sticks.Value stays at 0. And you were always adding 0 to Cash.

    This is likely because you are using a Tool to increment Sticks.Value, and it is common for Tools to use LocalScripts to hook up logic. When you make changes to the world (or leaderstats values) in LocalScripts, those changes are only present on that client. They are not replicated up to the server. And since all of your cash logic is in server scripts, the Sticks value stays at 0 on the server.

    So to fix this, you need to make sure that the code that increments Sticks.Value happens in a Script. And you can do that by using RemoteEvents in the same way you're using your BindableEvent, to communicate from the tool up to a server script.

    So in your Tool's LocalScript you'd do something like this :

    -- find the RemoteEvent saved in ReplicatedStorage
    local stickEvent = game.ReplicatedStorage.GetSticksEvent
    
    -- listen for when the Tool is used
    script.Parent.Activated:Connect(function()
        -- tell the server to give us some sticks...
        stickEvent:FireServer()
    end)
    

    Then, in a server Script, listen for that RemoteEvent to fire to give the player some sticks :

    -- find the RemoteEvent saved in ReplicatedStorage
    local stickEvent = game.ReplicatedStorage.GetSticksEvent
    
    -- listen for when the client tells us that they got some sticks
    stickEvent.OnServerEvent:Connect(function(player)
        -- give the player some sticks
        player.leaderstats.Sticks.Value += 1
    end)
    

    Edit - in this most recent version, your Sell script has stopped working because the player argument is nil. In an earlier version of this code, the Touched script passed in the player, but you removed that code and broke the Sell script. So just rollback the script to its earlier state :

    script.Parent.Touched:Connect(function(hit)
        if hit.Parent:FindFirstChild("Humanoid") then
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        
            game.ReplicatedStorage.BindableEvents.Sell:Fire(player)
        end
    end)