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

How to fix my value(inside character) set script?

I wrote a script so when part touched it does this:

collected = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:FindFirstChild("character") then
		if collected == false then
			collected = true
		hit.Parent.Energy.Value = hit.Parent.Energy.Value + 555
		hit.Parent.Humanoid:TakeDamage(-20)
		hit.Parent.Stamina.Value = hit.Parent.Stamina.Value + 50
		hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed + 1
		hit.Parent.Humanoid.JumpPower = hit.Parent.Humanoid.JumpPower + 1
		print("collected")
		for i = 0,1,.1 do
		script.Parent.Transparency = i
		script.Parent.Size = Vector3.new(i,i,i)
		wait()
		end
		script.Parent:Destroy()
		end
	end
end)

So the values dont function correctly

		hit.Parent.Energy.Value = hit.Parent.Energy.Value + 555
		--hit.Parent.Humanoid:TakeDamage(-20) works
		hit.Parent.Stamina.Value = hit.Parent.Stamina.Value + 50
		--hit.Parent.Humanoid.WalkSpeed = hit.Parent.Humanoid.WalkSpeed + 1 works
		--hit.Parent.Humanoid.JumpPower = hit.Parent.Humanoid.JumpPower + 1 works

The nergy Value inside player is 1000 i even CHECKED, and when this goes through (i made debug so i know this line is executed) it sets value to 555, not 1000 + 555,

hit.Parent.Energy.Value = hit.Parent.Energy.Value + 555

Before: energy.value is 1000 After: energy.value is 555 and not 1555.

Please help!

3 comments

0

When you changed your energy value to 1000, did you do it on the server (Script) or the client (LocalScript)?

0

Normal script, but if normal scritps dont work, why did the values change at all?

0

well initially my thought was you made the energy be 1000, on the client (so the server is 0), but upon trying this it seemed to work, mind if i have a look?

1 answer

The hit.Parent of the Touched Event is going to be the actual player's Character, not the player. As a result, when you check the values of "Energy" and "Stamina" they won't change since you're looking for the values as if they were inside of the character.

You can use the :GetPlayerFromCharacter() method to retrieve the player from the .Touched Event

You should also use :WaitForChild() to make sure the parts you are changing exist, since otherwise it returns a warning.

Revised Server Script

local collected = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		local humanoid = hit.Parent:WaitForChild("Humanoid")
		if collected == false and player then
			collected = true
			player:WaitForChild("Energy").Value = player:WaitForChild("Energy").Value + 555
			player:WaitForChild("Stamina").Value = player:WaitForChild("Stamina").Value + 50
			humanoid:TakeDamage(-20)
			humanoid.WalkSpeed = humanoid.WalkSpeed + 1
			humanoid.JumpPower = humanoid.JumpPower + 1
			print("collected")
			for i = 0,1,.1 do
				script.Parent.Transparency = i
				script.Parent.Size = Vector3.new(i, i, i)
				wait()
			end
			script.Parent:Destroy()
		end
	end
end)

3 comments

0

WHAT DO YOU MEAN, THEY DID CHANGE, sorry my bad, i said in player, i meant values are inside character

0

That certainly makes a difference....

0

so why does it change it to 555 instead of value (1000) + 555

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