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

A simple script I am working on is erroring yet I do not know why. Can anyone help?


local p = script.Parent

function onTouched(hit)
	local c = game.Players:GetPlayerFromCharacter(hit.Parent)
	if c then
		
	game.Players.c.Stage.Value = 1
	
	end
end

p.Touched:connect(onTouched)

script.Parent is a part with the name "Stage 1", which is a child of the model "StageNotifiers". This model is a child of the Workspace.

This script is (supposed to) set the leaderstat value of "Stage" to 1. I then will be copying this script into the part "Stage 2", located in the same model, changing the value inside of the script to "2", etc.

The purpose of this script is for an obstacle course that I'm working on.

Current error: "c is not a valid member of Players"

1 comment

0

Answered. Thanks!

2 answers

It's giving an error because you haven't checked if "hit" is a player yet. The script should look like this

local p = script.Parent

function onTouched(hit)
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
	    local c = game.Players:GetPlayerFromCharacter(hit.Parent)
	    if c then
	
	    game.Players.c.Stage.Value = 1
	
	    end
	end
end

p.Touched:connect(onTouched)

This will check if whatever touched script.Parent is a player or not. I hope this can help you out!

2 comments

0

I'll try this out, thanks!

0

I'm still being given the same error, strangely. I can't tell why.

@isaacsoccer, you didn't adress the error itself. "local c = game.Players:GetPlayerFromCharacter(hit.Parent)" is supposed to define c as the character of a player. Remember though, if an acessory hits the part, the part that touched it will be called Handle. Structure of the player accessory:

-- > Player1 --> Accessory --> Handle --> Upper torso --> Lower Torso etc

Check if the parent of the hit part is an accessory, before you call "GetPlayerFromCharacter(hit.Parent)". Also, you need to tidy up line 9.

"c" is defined as the player now. It DOES NOT represent THE NAME. Tidy this up to "c.Stage.Value = 1".

1 comment

0

Just saw this after posting my second reply to his. I'll try this out, thank you.

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