local DEX = nil local RD = math.random(1,6) game:GetService("Players").PlayerAdded:Connect(function(Player) local char = Player.Character local humanoid = char:WaitForChild("Humanoid") local humanoidrootpart = char:WaitForChild("HumanoidRootPart") DEX = RD *0.02 DEX = DEX * 16 script.DEX.Value = DEX end)
This is the script. What I am trying to do as I said in the title, is generate a random number then apply the outcome to a value that is parented to the script. Any help would be greatly appreciated.
Hello,
You haven't provided any information on the undesired outcome you're experiencing (errors, not running, etc), so this answer will have a few of my best guesses as to why it's not working.
First, in your PlayerAdded function, we need to add a check to make sure that the player's character has loaded:
local DEX = nil local RD = math.random(1,6) game:GetService("Players").PlayerAdded:Connect(function(Player) workspace:WaitForChild(Player.Name) --Ensures that Player.Character exists before finding it's children local char = Player.Character local humanoid = char:WaitForChild("Humanoid") local humanoidrootpart = char:WaitForChild("HumanoidRootPart") DEX = RD *0.02 DEX = DEX * 16 script.DEX.Value = DEX end)
We must do this because there is a buffer period between a client connecting to the server, and the server creating the player's character. An expected console error for not including this would be something along the lines of:
attempt to index nil with 'WaitForChild'
Secondly, double-check that the value inside your script is a NumberValue as opposed to an IntValue. We can check this using various methods, but the easiest is to click on the value object in your Explorer window, navigate to your properties window tab, and look under the property "ClassName". We can also check this using code:
print(workspace.SCRIPTNAME.DEX.ClassName)
If it appears that the value is NOT a NumberValue, then delete the object, and replace it with a different NumberValue object.
This check is important because IntValue only holds the value of integers, which CANNOT have any decimal places.
Third, check that your script is a Script, not a LocalScript, and verify that it is enabled/running. We can check very easily that a script is running by printing a string at runtime:
print("RNG SCRIPT WORKING") -- verifies that the script is running local DEX = nil local RD = math.random(1,6) game:GetService("Players").PlayerAdded:Connect(function(Player) workspace:WaitForChild(Player.Name) --Ensures that Player.Character exists before finding it's children local char = Player.Character local humanoid = char:WaitForChild("Humanoid") local humanoidrootpart = char:WaitForChild("HumanoidRootPart") DEX = RD *0.02 DEX = DEX * 16 script.DEX.Value = DEX end)
If we look in the console and do not see "RNG SCRIPT NOT WORKING", then we know that our script is not executing.
Hopefully, some of these strategies were able to make your code function as intended :)
Cheers, Ninjamanchase