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

Awarding Points For Jumping and Landing - humanoid.StateChanged - OK in Local Script?

Asked by 4 years ago

My second very rookie question here, and I’ll preface by acknowledging that the answer is probably: “Go learn client vs server / local vs normal script and come back.”

I am working on a scoring system that awards points after a player jumps and lands. I’m also adding a multiplier that awards additional points after landing if the player first touched a part. I have a leaderstat called Experience that is increased by 10 for each landing, unless the player has touched a “tricktoken” which makes the landing worth 20. Again, I only want to award points after landing, but only after a jump has occurred (if you were to run off a building and land, that does not count).

So I have the leaderstat called Experience, and player values for JumpCheck and Multiplier. I’m using the humanoid.StateChanged event to check for the ‘Jumping’ and ‘Landed’ states and doing the following: After a jump, I set the ‘JumpCheck’ value from 0 to 1. After a landing I award 10 points times the JumpCheck value, and the Multiplier value. After awarding the points the JumpCheck goes back to zero and the Trick Multiplier back to 1.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(script.Parent)
local Multiplier = player.Multiplier.Value
local JumpCheck = player.JumpCheck.Value

humanoid.StateChanged:Connect(function(oldState, newState)
    if newState == Enum.HumanoidStateType.Jumping then
        print("Player Just Jumped - Setting JumpCheck to 1")
        JumpCheck = 1
        end
    end)

humanoid.StateChanged:Connect(function(oldState, newState)
    if newState == Enum.HumanoidStateType.Landed then
        print("Player just landed. If JumpCheck was 1, award 10 * the point multiplier, then reset multiplier back to 1 and jumpcheck to 0")
        player.leaderstats.Experience.Value = player.leaderstats.Experience.Value + (10 * JumpCheck * Multiplier)
        Multiplier = 1
        JumpCheck = 0
    end
end)

All of this works well to award the normal points; 10 at a time only, but I’m sure its not optimal, and the multiplier does not work, and I’m guessing these two issues are related.

Q1: This works to award 10 points with each jump in a local script in StarterCharacterScripts. If I use a regular script, the points are only awarded sporadically/infrequently. I believe this is due to the short time that the HumanoidStateType of Jumping and Landing last – perhaps not being picked up server-side. Is having these points awarded via local script in the character bad practice?

Q2: I’ve been unable to increase my point multiplier by touching the part in the game. I’ve tried a Touched event script inside the part in the Workspace, but have been unable to actually grant the multiplied points. My desired outcome is: You hit the part, set player.Multiplier.value to 2, then you jump and set player.JumpCheck.Value to 1, then you landed and awarded 10 * Mutiplier(2) * JumpCheck(1) = 20. I'm again guessing this doesn't work due to being handled on the server (part in the Workspace)?

This is inside the part:

db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if db == false then
            db = true
            player.Multiplier.Value = 2
            wait(1)
            db = false
        end
    end
end)

If you made it this far, apologies for the length – I’m perfectly fine with the appropriate answer: “You’re out of your depth man – go research client, server, remote events, etc...and come back.”

Cheers, Jeff

1 answer

Log in to vote
0
Answered by 4 years ago

Aha, I see the problem! What you're doing there seems to be very creative and fun, but there is just a slight mistake that you made. At the top of the script, you are saying

local Multiplier = player.Multiplier.Value

The problem with that is it will set the variable "multiplier" to whatever multiplier is when the script starts. If you want it to always be updating, store the variable in object form instead of number, so it can auto-update the value.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local player = game.Players:GetPlayerFromCharacter(script.Parent)
local Multiplier = player.Multiplier
local JumpCheck = player.JumpCheck

humanoid.StateChanged:Connect(function(oldState, newState)
    if newState == Enum.HumanoidStateType.Jumping then
        print("Player Just Jumped - Setting JumpCheck to 1")
        JumpCheck.Value = 1
        end
    end)

humanoid.StateChanged:Connect(function(oldState, newState)
    if newState == Enum.HumanoidStateType.Landed then
        print("Player just landed. If JumpCheck was 1, award 10 * the point multiplier, then reset multiplier back to 1 and jumpcheck to 0")
        player.leaderstats.Experience.Value = player.leaderstats.Experience.Value + (10 * JumpCheck.Value * Multiplier.Value)
        Multiplier.Value = 1
        JumpCheck.Value = 0
    end
end)

If you have any more problems, feel free to respond.

0
Oh my GOSH!!! You are of course exactly right, and now the strange behavior, where my multiplier would stay at 1 (which is the initial value that I was setting the variable to) makes perfect sense. I can't thank you enough for taking the time to read all of this and craft the response. I'm so happy this is working right now!!! Thank you very very much. Tricky_Itch 10 — 4y
Ad

Answer this question