How to change this line of code?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want it so this line of code is implemented, but I’m not sure how to do it.

  2. What is the issue? Include screenshots / videos if possible!
    I can’t figure it out.
    I want to implement this:

plr.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried implementing it, but it didn’t work.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local SS = game:GetService("ServerStorage")
local Cloneables = SS.Cloneables
local tc_Train = Cloneables.Train
local RS = game:GetService("ReplicatedStorage")
local REs = RS.REs
local rE_Spawn = REs.SpawnTrain
local spawnParts = game.Workspace.SpawnParts:GetChildren()

local currentSpawnIndex = 1

local function CloneTrain(plr)
    local spawnPart = spawnParts[currentSpawnIndex]

    local train = tc_Train:Clone()
    train.Parent = game.Workspace
    train:SetPrimaryPartCFrame(spawnPart.CFrame)

    -- Remove player from previous seat if they are seated
    local previousSeat = plr.Character.Humanoid.SeatPart
    if previousSeat then
        plr.Character.Humanoid.Sit = false
    end

    -- Teleport the player to the spawn part and make them sit in the VehicleSeat
    local vehicleSeat = train.PrimaryPart:FindFirstChild("VehicleSeat")
    if vehicleSeat then
        plr.Character.HumanoidRootPart.CFrame = vehicleSeat.CFrame
        vehicleSeat:Sit(plr.Character.Humanoid)
        
        -- Set the IsSitting attribute and disable jumping
        plr.Character:SetAttribute("IsSitting", true)
        plr.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
    end

    -- Add more SpawnIndexes and change back to original if full
    currentSpawnIndex = currentSpawnIndex + 1
    if currentSpawnIndex > #spawnParts then
        currentSpawnIndex = 1
    end
end

rE_Spawn.OnServerEvent:Connect(CloneTrain)
1 Like

Are you experiencing any errors? What’s the issue?

You will have to call this locally I think.

[EDIT] @HugeCoolboy2007, I meant you will have to call the OP’s code to disable the Humanoid’s jump in a LocalScript and not in a Script on the server.

1 Like

No, OnServerEvent is a server connection

1 Like

Slight issue all, I want to implement this line of code, I made a mistake in the original post.

humanoid.usejumppower = true humanoid.jumppower = 0

When I put the code in, I get this error.

Along with this in my script.
Screenshot 2023-07-29 223458

Humanoid is not defined correctly + The property name is UseJumpPower not usejumppower

Screenshot 2023-07-29 224049
Would that be correct?

plr.Character.Humanoid.UseJumpPower = true
plr.Character.Humanoid.JumpPower = 0

Seems like it was as simple as that, thanks.

Unfortunately, even though the quick fix may work, the solution isn’t as simple as switching one line. Luckily, the script is not complicated, and everything can be neatly resolved.

I truly recommend spelling the names out and making them as descriptive as possible. I did a slight rewriting of your code with replaced naming, if you don’t mind.

Additionally, I don’t know why use JumpPower when JumpHeight is default. Furthermore, it doesn’t make much sense for this script to switch to JumpPower. It can break other scripts relying on jump height later on. Unless of course this is a convention for all your scripts currently in game and the ones to come. Either way, this script should not be the one opting for JumpPower.

Using plr.Character.HumanoidRootPart also has possible exceptions where the root part doesn’t exist, just like the humanoid. Exploiters will be able to raise errors.

local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remotes = ReplicatedStorage.REs -- folder with remote events?
local spawnTrainRE = remotes.SpawnTrain

local spawnPartList = workspace.SpawnParts:GetChildren() -- an array of all spawn points
local cloneables = ServerStorage.Cloneables
local train = cloneables.Train -- the train model

local currentSpawnIndex = 1

local function CloneTrain(player)
	-- Check if character even exists. It doesn't? Decline (ignore) the request.
	if not player.Character then
		return;
	end
	
	-- Are humanoid and humanoid root part to be found in the character?
	-- No? Decline.
	local humanoid = player.Character:FindFirstChild("Humanoid")
	local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
	if not humanoid or not humanoidRootPart then
		return;
	end
	
	-- Choose the spawn part, which is at 'currentSpawnIndex' in the list of spawn points.
	local spawnPart = spawnPartList[currentSpawnIndex]
	
	-- Close a new train.
	local newTrain = train:Clone()
	newTrain.Parent = workspace
	newTrain:PivotTo(spawnPart.CFrame) -- SetPrimaryPartCFrame() is deprecated
	
	-- If player has been seating during the request, lift them up.
	local previousSeat = humanoid.SeatPart
	if previousSeat then
		humanoid.Sit = false
	end
	
	-- Does the train have a vehicle seat?
	-- If it does, sit the humanoid and disable jumping.
	local vehicleSeat = newTrain.PrimaryPart:FindFirstChild("VehicleSeat")
	if vehicleSeat then
		humanoidRootPart.CFrame = vehicleSeat.CFrame
		vehicleSeat:Sit(humanoid)
		
		player.Character:SetAttribute("IsSitting", true)
		-- You should probably use 'JumpHeight' instead, but the alternative
		-- is in the comments. It's not default, however, and doesn't make much sense
		-- for this script to change this setting, possibly affecting other scripts.
		humanoid.JumpHeight = 0
		--humanoid.UseJumpPower = true
		--humanoid.JumpPower = 0
	end
	
	-- Increment the index in a list.
	currentSpawnIndex += 1
	if currentSpawnIndex > #spawnPartList then
		currentSpawnIndex = 1
	end
end

spawnTrainRE.OnServerEvent:Connect(CloneTrain)

I haven’t had a chance to test this, so please let me know if there are any errors. My DMs are open too.


Btw, as for the original SetStateEnabled() line, @dduck5tar was right, setting states won’t replicate, and local changes are required.

PS: This reply will probably never be noticed lol. But if it is, good luck.

2 Likes

Oh wow! You re-wrote it, and it looks 100x better. This works nicely, with no errors. I do appreciate this, as I am still on my way to improving as a scripter. Thanks!

2 Likes

Top notch reply @MeerkatSpirit! I know it’s past the ‘topic solved’ deadline, but what the hell! I did notice the reply, and as such offer my doffed hat to you.

2 Likes

Oh, well, thank you :slight_smile: The new version isn’t actually so different from the original but thanks. You actually explained why the original didn’t work.

@AstronautJaye np! I added some comments for explanation. The whole process is quite straightforward and can be translated into English for easier understanding. Hopefully that helps.


I just noticed a minor overlook on my account too. If the vehicle seat is not found and the humanoid was seated at the time of the request, IsSitting attribute of player.Character would remain unchanged.

The missing line right here:


Otherwise,

the reply could probably be more polished by mentioning cooldowns to prevent the spamming of remotes.

However, the rest (even including the cooldowns in practice) likely well exceeds this topic and is hard to suggest without knowing more about the whole system and how these trains work.

  • What if train despawning part is handled elsewhere? What if the train is scheduled for removal after being left inactive for a period of time?

  • Maybe train ownership could be introduced. That way we would be able to remove the old train on each new request. This requires a way to associate a train with its owner, like a table, an attribute, a value base…

New version of the script (with a 60-second cooldown between requests)
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TRAIN_SPAWN_COOLDOWN_TIME = 60 --// seconds

local remotes = ReplicatedStorage.REs -- folder with remote events
local spawnTrainRE = remotes.SpawnTrain

local spawnPartList = workspace.SpawnParts:GetChildren() -- an array of all spawn points
local cloneables = ServerStorage.Cloneables
local train = cloneables.Train -- the train model

local playerInCooldown = {} -- {[Player] = true/nil}

local currentSpawnIndex = 1

local function CloneTrain(player)
	-- Check if character even exists. It doesn't? Decline (ignore) the request.
	if not player.Character then
		return;
	end

	-- Are humanoid and humanoid root part to be found in the character?
	-- No? Decline.
	local humanoid = player.Character:FindFirstChild("Humanoid")
	local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
	if not humanoid or not humanoidRootPart then
		return;
	end
	
	-- Is player currently in the cooldown process?
	-- If so, decline the request, otherwise add then to the cooldown
	-- process and continue with train spawning.
	if playerInCooldown[player] then
		return;
	else
		playerInCooldown[player] = true
	end

	-- Choose the spawn part, which is at 'currentSpawnIndex' in the list of spawn points.
	local spawnPart = spawnPartList[currentSpawnIndex]

	-- Clone a new train.
	local newTrain = train:Clone()
	newTrain.Parent = workspace
	newTrain:PivotTo(spawnPart.CFrame) -- SetPrimaryPartCFrame() is deprecated

	-- If player has been seating during the request, lift them up.
	local previousSeat = humanoid.SeatPart
	if previousSeat then
		humanoid.Sit = false
		player.Character:SetAttribute("IsSitting", false)
	end

	-- Does the train have a vehicle seat?
	-- If it does, sit the humanoid and disable jumping.
	local vehicleSeat = newTrain.PrimaryPart:FindFirstChild("VehicleSeat")
	if vehicleSeat then
		humanoidRootPart.CFrame = vehicleSeat.CFrame
		vehicleSeat:Sit(humanoid)

		player.Character:SetAttribute("IsSitting", true)
		
		humanoid.JumpHeight = 0
		--humanoid.UseJumpPower = true
		--humanoid.JumpPower = 0
	end

	-- Increment the index in a list.
	currentSpawnIndex += 1
	if currentSpawnIndex > #spawnPartList then
		currentSpawnIndex = 1
	end
	
	task.wait(TRAIN_SPAWN_COOLDOWN_TIME)
	playerInCooldown[player] = nil -- out of the cooldown table
end

spawnTrainRE.OnServerEvent:Connect(CloneTrain)

→ The cooldown uses a dictionary, but it might as well use arrays with table.insert(tbl, player) for adding and table.remove(tbl, table.find(tbl, player)) for removing.

→ Storing the request time and comparing it to the current time is an option too.

Same as before, there could be a hidden mistake, I’m here if the script doesn’t work.

We have intentions to add a system where if they don’t move for say two minutes, they’ll be ghosted or removed from the map and put back in the menu.

We used to have an IntValue inside of the train, for the owner. However that has seems to of been removed. I have intentions to re-add this with a menu button, where if you press that your train gets removed and respawned on a new request.

This new script does work!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.